package com.yixun.lbx.controller.farm;

import com.yixun.controller.BaseController;
import com.yixun.lbx.entity.YxProBreedMedication;
import com.yixun.lbx.entity.YxProCompany;
import com.yixun.lbx.service.BreedMedicationService;
import com.yixun.lbx.service.CompanyService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/*
 * 用药记录 Controller
 * */
@RestController
@RequestMapping("/bm")
public class BreedMedicationController extends BaseController {
    private final BreedMedicationService medicationService;
    private final CompanyService companyService;

    public BreedMedicationController(CompanyService companyService, BreedMedicationService medicationService) {
        this.companyService = companyService;
        this.medicationService = medicationService;
    }

    /*
     *获取用药记录列表
     */
    @PostMapping("/getListByPage")
    public HashMap<String, Object> getListByPage(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        Integer count = medicationService.getCount(map);
        List<YxProBreedMedication> medications = medicationService.getListByPage(map);

        map.put("count", count);
        map.put("medications", medications);
        return map;
    }

    /*
     *添加用药记录
     */
    @PostMapping("/addMedication")
    public int addMedication(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        map.put("addUser", this.getSessionUser().getUser_id());
        return medicationService.addMedication(map);
    }

    /*
     *修改用药记录
     */
    @PostMapping("/updateMedication")
    public int updateMedication(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        return medicationService.updateMedication(map);
    }

    /*
     *删除用药记录
     */
    @PostMapping("/deleteMedication")
    public int deleteMedication(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        String[] ids = map.get("id").toString().split(",");
        map.put("ids", ids);
        return medicationService.deleteMedication(map);
    }

    @GetMapping("/getObjByArgs")
    public List<YxProBreedMedication> getObjByArgs(YxProBreedMedication medication) {
        return medicationService.getObjByArgs(medication);
    }

    /*搜索*/
    @RequestMapping("/getSearchListByPage")
    public HashMap<String, Object> getSearchListByPage(@RequestBody HashMap<String, String> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", String.valueOf(qyId));
        }
        List<YxProBreedMedication> objs = medicationService.getSearchListByPage(map);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("obj", objs);
        hashMap.put("count", objs.size());
        return hashMap;
    }

    private Long getQyId() {
        YxProCompany yxProCompany = new YxProCompany();
        yxProCompany.setCode(this.getSessionUser().getUser_name());
        YxProCompany company = companyService.getCompanyByIdOrNameOrCode(yxProCompany);
        if (company != null) {
            return company.getQyId();
        } else {
            return 0L;
        }
    }
}
